package Demo73;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 验证栈序列
 * https://leetcode.cn/problems/validate-stack-sequences/
 */
class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Deque<Integer> deque = new ArrayDeque<>();
        int len = pushed.length;
        int j = 0;
        for(int i = 0; i < len;i++) {
            deque.push(pushed[i]);
            while(j < len && !deque.isEmpty() && popped[j] == deque.peek()) {
                deque.poll();
                j++;
            }
        }
        if(deque.isEmpty()) {
            return true;
        }
        return false;
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        boolean b = solution.validateStackSequences(new int[]{1,0}, new int[]{1,0});
        System.out.println(b);
    }
}